package ca.etsmtl.log430.lab2;

/**
 * This class is used by various other classes that need to keep a list of
 * courses. It extends the List class which provides the basic functionality for
 * storage and retrieval of the Course Object from the list.
 * 
 * @author A.J. Lattanze, CMU
 * @version 1.2, 2011-Feb-02
 */

/*
 * Modification Log
 * ****************************************************************************
 * v1.2, 2011-Feb-02, R. Champagne - Various refactorings, javadoc comments.
 * 
 * v1.1, 2002-May-21, R. Champagne - Adapted for use at ETS.
 * 
 * v1.0, 12/29/99, A.J. Lattanze - Original version.
 * ***************************************************************************
 */

public class CourseList extends List {

	public CourseList() {

		super();

	} // Constructor

	/**
	 * @param CourseObject
	 *            New course to be added to the list. All the issues of casting
	 *            are taken care of within this class.
	 */
	public void addCourse(Course CourseObject) {

		appendItemToList((Object) CourseObject);

	} // AddCourse

	/**
	 * @return The course pointed at the current position pointed to by the
	 *         internal list pointer of the internal list. Subsequent calls will
	 *         return the next Course object in the list. A null object is
	 *         returned if list is empty or the end of list has been reached.
	 */
	public Course getNextCourse() {

		return (Course) getItemFromList();

	} // GetNextCourse

	/**
	 * This method assumes that all courses have different identification
	 * numbers.
	 * 
	 * @param CourseObject
	 * @return A Course instance if found in the list based on specified
	 *         criteria, null otherwise.
	 */
	public boolean findCourse(Course CourseObject) {

		Course CurrentObject;
		boolean Done = false;
		boolean Result = false;

		goToFrontOfList();

		while (!Done) {

			CurrentObject = getNextCourse();

			if (CurrentObject == null) {

				Done = true;

			} else {
				if (CourseObject.getCourseID().compareToIgnoreCase(
						CurrentObject.getCourseID()) == 0) {

					Result = true;

				} // if

			} // if

		} // while

		return (Result);

	} // FindCourse

	/**
	 * This method assumes that all courses have different identification
	 * numbers.
	 * 
	 * @param CourseID
	 * @param Section
	 * @return A Course instance if found in the list based on specified
	 *         criteria, null otherwise.
	 */
	public Course findCourseByIDAndSection(String CourseID, String Section) {

		Course CurrentObject = null;
		boolean Done = false;
		boolean Found = false;

		goToFrontOfList();

		while (!Done) {

			CurrentObject = getNextCourse();

			if (CurrentObject == null) {

				Done = true;

			} else {

				if ((CurrentObject.getCourseID().compareToIgnoreCase(CourseID) == 0)
						&& (CurrentObject.getSection().compareToIgnoreCase(
								Section) == 0)) {

					Found = true;
					Done = true;

				} // if

			} // if

		} // while

		if (Found) {

			return (CurrentObject);

		} else {

			return (null);

		} // if

	} // FindCourseByIDAndSection
	
	public Course findCourseByID(String CourseID) {

		Course CurrentObject = null;
		boolean Done = false;
		boolean Found = false;

		goToFrontOfList();

		while (!Done) {

			CurrentObject = getNextCourse();

			if (CurrentObject == null) {

				Done = true;

			} else {

				if ((CurrentObject.getCourseID().compareToIgnoreCase(CourseID) == 0)) {

					Found = true;
					Done = true;

				} // if

			} // if

		} // while

		if (Found) {

			return (CurrentObject);

		} else {

			return (null);

		} // if

	} // FindCourseByIDAndSection

} // CourseList
